package com.pagepub.commons;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncondingFilter implements Filter {

	private String encodingStr;

	/**
	 * 无参构造方法.
	 * 
	 */
	public EncondingFilter() {
	}

	/**
	 * 过滤器初始化方法.
	 * 
	 * @param fc
	 *            读取配置文件参数.
	 */
	public void init(FilterConfig fc) throws ServletException {
		encodingStr = fc.getInitParameter("encoding");
	}

	/**
	 * servlet的doFilter()方法.
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(encodingStr);
		response.setCharacterEncoding(encodingStr);
		chain.doFilter(request, response);
	}

	/**
	 * 过滤器销毁方法.
	 */
	public void destroy() {

	}
}
